﻿CREATE PROCEDURE [dbo].[UpdatePerson]
    @PersonId uniqueidentifier,
    @LastName nvarchar(50),
    @FirstName nvarchar(50),
    @MiddleName nvarchar(50),
    @Photo varbinary(max),
	@Email varchar(50),
	@UserType uniqueidentifier,
	@PasswordHash nvarchar(256),
	@PasswordSalt nvarchar(256),
	@Login nchar(40),
	@Comment nvarchar(max),
    @IsDeleted bit,
    @LastChanged datetime,
	@LastChangedBy uniqueidentifier,

	@TimeStamp timestamp,
	@newTimeStamp timestamp output

AS

	UPDATE Person

	SET 

	 LastName = @LastName,
	 FirstName = @FirstName,
	 MiddleName = @MiddleName,
	 Photo = @Photo,
	 Email = @Email,
	 UserType = @UserType,
	 PasswordHash = @PasswordHash,
	 PasswordSalt = @PasswordSalt,
	 [Login] = @Login,
	 Comment = @Comment,
	 IsDeleted = @IsDeleted, 
	 LastChanged = @LastChanged,
	 LastChangedBy = @LastChangedBy

	WHERE PersonId = @PersonId AND [TimeStamp]=@TimeStamp

  IF @@ROWCOUNT = 0

    RAISERROR('Row has been edited by another user', 16, 1)            	


	SELECT @newTimeStamp = [TimeStamp]

	FROM Person 

	WHERE PersonId = @PersonId

RETURN 0